﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BallotCounter.Question;

namespace BallotCounter.Answer
{
    class Sofia:IBallotCounter
    {
        public int BallotCount(int[] ballotArray)
        {
            int result = 0;
            bool flag = true;
            SortedList ht = new SortedList();
            for (int i = 0; i < ballotArray.Length; i++)
            {
                if (ballotArray[i] < 0)
                {
                    result = -1;
                    flag = false;
                    break;
                }
                if (ht.ContainsKey(ballotArray[i]))
                {
                    int counter = (int)ht[ballotArray[i]];
                    counter++;
                    ht[ballotArray[i]] = counter;
                }else
                    ht.Add(ballotArray[i], 1);
            }

            if (flag == true)
            {
                if (ht.Count > 1 && (int)ht[1] == (int)ht[ht.Count]) result = -1; 
                else result = (int)ht.GetKey(0);    
            }
      
            return result;
        }
    }
}
